package com.ukao.pad.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.ukao.pad.R;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.DensityUtils;
import com.ukao.pad.widget.CustomPopWindow;
import com.ukao.pad.widget.StateImageView;

import java.text.ParseException;
import java.util.Calendar;

import me.yokeyword.fragmentation.SupportActivity;

import static com.ukao.pad.utils.MyDateUtils.getTime;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class DialogTool {


    private volatile static DialogTool singleton;

    private TimePickerView pvCustomTime;
    private android.widget.TextView lossinformation;
    private Button cancel;
    private Button submit;
    
    public DialogTool(){

    }
    public static DialogTool getSingleton() {
        if (singleton == null) {
            synchronized (DialogTool.class) {
                if (singleton == null) {
                    singleton = new DialogTool();
                }
            }
        }
        return singleton;
    }



    /**
     * 入厂成功
     */
    public CustomPopWindow showPopEnterFactory(boolean isleveEnter,final SupportActivity _mActivity, View  parent) {
        View contentView = LayoutInflater.from(_mActivity).inflate(R.layout.popwin_enter_factory_succed, null);
        ImageView     image=(ImageView)contentView.findViewById(R.id.image);
        if (isleveEnter){
            image.setImageResource(R.drawable.lever_factory_succeed);
        }
        final    CustomPopWindow mEnterFactoryWindow = new CustomPopWindow.PopupWindowBuilder(_mActivity)
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .create()
                .showAtLocation(parent, Gravity.CENTER, 0, 0);


        try {
            if (!_mActivity.isFinishing())
            parent.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!_mActivity.isFinishing())
                        mEnterFactoryWindow.dissmiss();
                }
            }, 1000);
        }catch (Exception e){

        }


        return mEnterFactoryWindow;
    }

    /**
     * 是否上报缺失
     */
    private Button editsexsubmit;
    private Button editsexcancel;
    public CustomPopWindow showPopReportLoss(Context _mActivity, View  parent,String  message, View.OnClickListener  mOnClickListener) {
        View contentView = LayoutInflater.from(_mActivity).inflate(R.layout.popwin_report_loss, null);
        this.submit = (Button) contentView.findViewById(R.id.submit);
        this.cancel = (Button) contentView.findViewById(R.id.cancel);
        this.lossinformation = (TextView) contentView.findViewById(R.id.loss_information);
        this.editsexsubmit= (Button) contentView.findViewById(R.id.submit);
        this.editsexcancel= (Button) contentView.findViewById(R.id.cancel);

        if (!CheckUtils.isEmpty(message))lossinformation.setText(message);
       final   CustomPopWindow   mReportLossWindow = new CustomPopWindow.PopupWindowBuilder(_mActivity)
                .setView(contentView)
               .enableOutsideTouchableDissmiss(false)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .size(DensityUtils.dip2px(500), DensityUtils.dip2px(320))
                .create()
                .showAtLocation(parent, Gravity.CENTER, 0, 0);
        editsexcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mReportLossWindow.dissmiss();
            }
        });
        editsexsubmit.setOnClickListener( mOnClickListener);

            return mReportLossWindow ;
    }

    /**
     * 下单时间段
     * @param showDays
     * @param mRightBt3
     * @param mRightBt1
     * @param mContext
     */
    public void showDateQuantumPicker(final int showDays,final Button mRightBt3,final  Button mRightBt1,Context mContext,
                                      TimePickerView.OnTimeSelectListener mOnTimeSelectListener) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();
        try {

            String time = showDays > 7 ? mRightBt3.getText().toString() : mRightBt1.getText().toString();
            time = Calendar.getInstance().get(Calendar.YEAR) + "—" + time;
            selectedDate.setTime(getTime(time, "yyyy—MM—dd EEE"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        startDate.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, showDays);
//        endDate.set(Calendar.getInstance().get(Calendar.YEAR), 12,31);
        //时间选择器 ，自定义布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_basepickerview, null);
        pvCustomTime = new TimePickerView.Builder(mContext, mOnTimeSelectListener
        )

                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .isDialog(true)
                .setLabel("", "月", "日", "", "", "")
                .setTextColorCenter(Color.BLACK)
                .setContentSize( CheckUtils.isMIUIDevices()?17:26)
                .setDecorView((ViewGroup) view)
                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(R.id.close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.tv_title);

                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });

                        picker_title.setText(showDays > 6?"送件日期":"取件日期");
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }

                })
                .setType(new boolean[]{false, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(R.color.gray)
                .build();
        pvCustomTime.show();

    }

    public void showDialog(Context  mContext,String tipaymentPricetle,String sendCarriage,String takeCarriage ,String  modifyPrice,final  DialogInterface.OnCancelListener   mOnCancelListener){


        final Dialog dialog =
                new Dialog(mContext,R.style.MyDialog);
        dialog.setCanceledOnTouchOutside(true);
        View   view=LayoutInflater.from(mContext).inflate(R.layout.dialog_amount_of_detail,null);

        dialog.setContentView(view);
        dialog.setOnCancelListener(mOnCancelListener
        );



        StateImageView closeBtn = (StateImageView) view.findViewById(R.id.close_btn);
        TextView  title = (TextView) view.findViewById(R.id.tv_title);
        LinearLayout chagePiceLayout = (LinearLayout) view.findViewById(R.id.chage_pice_layout);
        TextView  chagepice = (TextView) view.findViewById(R.id.chage_pice);
        TextView   dialogTotalClothing= (TextView) view.findViewById(R.id.dialog_total_clothing);
        TextView   dialogPickUpFee= (TextView) view.findViewById(R.id.dialog_pick_up_fee);
        TextView   dialogDeliveryFee= (TextView) view.findViewById(R.id.dialog_delivery_fee);
        title.setText("金额明细");
        dialogTotalClothing.setText("￥"+tipaymentPricetle);
        dialogPickUpFee.setText("￥"+takeCarriage);
        dialogDeliveryFee.setText("￥"+sendCarriage);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnCancelListener.onCancel(dialog);
                dialog.dismiss();

            }
        });
        if (CheckUtils.isEmpty(modifyPrice)) {chagePiceLayout.setVisibility(View.GONE);}else{chagePiceLayout.setVisibility(View.VISIBLE); chagepice.setText("￥"+modifyPrice);}



        dialog.show();
    }
    public  void showDatePicker(String birthday,String timeType, Context  mContext,TimePickerView.OnTimeSelectListener mOnTimeSelectListener ) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        //系统当前时间
        Calendar selectedDate = Calendar.getInstance();

        try {


            if (!TextUtils.isEmpty(birthday)){
                selectedDate.setTime(getTime(birthday, timeType));
            }else{
                selectedDate.add(Calendar.YEAR,-25);
                selectedDate.setTime(selectedDate.getTime());
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar startDate = Calendar.getInstance();
        //0代表1月
        startDate.set(1900,0,1);
        Calendar endDate = Calendar.getInstance();
//        endDate.set(Calendar.getInstance().get(Calendar.YEAR), 11,31);
        //时间选择器 ，自定义布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_basepickerview, null);
        pvCustomTime= new TimePickerView.Builder(mContext, mOnTimeSelectListener)
                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)
                .isDialog(true)
                .setLabel("年", "月", "日", "", "", "")
                .setTextColorCenter(Color.BLACK)
                .setContentSize( CheckUtils.isMIUIDevices()?17:26)
                .setDecorView((ViewGroup) view)

                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        final Button btSubmit = (Button) v.findViewById(R.id.picker_submit);
                        final Button btCancel = (Button) v.findViewById(R.id.picker_cancel);
                        final StateImageView closeBtn = (StateImageView) v.findViewById(R.id.close_btn);
                        final TextView picker_title = (TextView) v.findViewById(R.id.tv_title);
                        picker_title.setText("添加生日");
                        closeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                        btSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.returnData();
                                pvCustomTime.dismiss();
                            }
                        });
                        btCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomTime.dismiss();
                            }
                        });
                    }

                })

                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(R.color.gray)
                .build();
        pvCustomTime.show();

    }

}
